<template>
  <div>
    <!-- 头部导航 -->
    <van-nav-bar title="登录" class="navtop"> </van-nav-bar>
    <!-- 验证码表单 -->
    <van-form @submit="onSubmit" class="from" ref="nameA">
      <!-- 提交事件使用name属性.没有则不能提交
        rules表单效验规则,数组里的每一个对象代表以个规则 -->
      <van-field
        v-model="username"
        name="手机号"
        placeholder="手机号"
        :rules="mobile"
      >
        <template #label>
          <span class="toutiao toutiao-shouji"></span
        ></template>
      </van-field>
      <van-field
        v-model="password"
        type="text"
        name="验证码"
        placeholder="验证码"
        :rules="yanzhen"
      >
        <template #label
          ><span class="toutiao toutiao-yanzhengma"></span
        ></template>

        <template #button>
          <van-button
            v-if="isshow"
            type="default"
            native-type="button"
            class="colo"
            round
            size="small"
            @click="sumi"
            >默认按钮</van-button
          >
          <van-count-down
            v-else
            :time="60 * 1000"
            format="ss 秒"
            @finish="isshow = true"
          />
        </template>
      </van-field>
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { loginApi, getCoudeApi } from '@/api'
import { mapMutations } from 'vuex'

export default {
  data() {
    //判断手机号是否违规
    const mobile = [
      { required: true, message: '请填写手机号', trigger: 'onChange' },
      {
        pattern:
          /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
        message: '请正确填写'
      }
    ]
    //判断验证码是否违规
    const yanzhen = [{ required: true, message: '请填写验证码' }]
    return {
      username: '',
      password: '',
      mobile,
      yanzhen,
      isshow: true
    }
  },
  methods: {
    //from表单提交触发验证
    ...mapMutations(['SET_TOKEN']),
    async onSubmit() {
      //使用vant组件登录提示
      const { username, password } = this
      this.$toast.loading({
        message: '登陆中...',
        forbidClick: true //弹窗时不允许点击
      })
      try {
        //发起网络请求
        const { data } = await loginApi(username, password)
        // this.$store.commit('SET_TOKEN',data.data)
        this.SET_TOKEN(data.data)

        this.$router.push('/my')
        this.$toast.success('登录成功')
      } catch (error) {
        //失败以后执行此提示
        // ?. 可选链  为undnfexd 时不继续执行
        if (error.response?.status === 400) {
          this.$toast.fail(error.response.data.message)
        } else {
          throw error
        }
      }
    },
    //验证码校验
    async sumi() {
      //确保填写有手机号并且有效
      try {
        await this.$refs.nameA.validate('手机号')

        //倒计时开始
        this.isshow = false
        //提示信息
        this.$toast.loading({
          message: '发送验证中...',
          forbidClick: true //弹窗时不允许点击
        })
        //有效以后进行登录处理
        await getCoudeApi(this.username)
        this.$toast.success('发送成功')
      } catch (error) {
        if (error.response?.status === 429) this.$toast.fail(error.response.data.message)
      }
    }
  }
}
</script>

<style scoped lang="less">
.navtop {
  background-color: #6db4fb;
  //穿透, scped使得是能当前组件使用当前类型,deep()可以使子元素穿透
  :deep(.van-nav-bar__title) {
    color: #fff;
  }
}
:deep(.from) {
  .toutiao {
    font-size: 40px;
  }
  .van-cell__title {
    flex: 1;
  }
  .van-cell__value {
    flex: 20;
  }
}

.colo {
  color: #9a8b7e;
  background-color: #eee;
  height: 0.64rem;
}
</style>
